package com.example.springboot.RabbitMQ;

import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class TestProducer_01 {

    private static final String exchangeName = "exchange_Direct_01";
    private static final String queueName_01 = "queue_Direct_01";
    private static final String queueName_02 = "queue_Direct_01";
    private static final String routingKey_01 = "routingKey_Direct_01";
    private static final String routingKey_02 = "routingKey_Direct_02";


    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMsg() {

        for (int i = 0; i < 100; i++) {
            String msg = "简单模式下的消息 " + i;
            System.out.println("简单模式下的消息 " + i);
            rabbitTemplate.convertAndSend(exchangeName,routingKey_01, msg);
        }
    }

    // 测试消息超时（消息层面）
    public void sendMsg2() {

        // 给消息添加超时时间
        MessagePostProcessor postProcessor = message -> {

            message.getMessageProperties().setExpiration("4000");

            return message;
        };

        String msg = "简单模式下的消息";
        rabbitTemplate.convertAndSend(exchangeName,routingKey_01, msg, postProcessor);

    }

    public void sendMsg3() {

        for (int i = 0; i < 100; i++) {
            String msg = "简单模式下的消息 " + i;
            System.out.println("简单模式下的消息 " + i);
            rabbitTemplate.convertAndSend(exchangeName,routingKey_02, msg);
        }

    }

    public void sendMsg4() {

            String msg1 = "发送消息 01";
            String msg2 = "发送消息 02";
            rabbitTemplate.convertAndSend(exchangeName,routingKey_01, msg1);

            int i = 1/0;

            rabbitTemplate.convertAndSend(exchangeName,routingKey_01, msg2);

        }

}